//
//  HSMainMeVC.m
//  HolyShit
//
//  Created by Robin on 2017/3/28.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSMainMeVC.h"
#import "HSLoginVC.h"
#import "HSCollectionVC.h"
#import "ParallaxHeaderView.h"
#import "HSSettingVC.h"
#import "HSMessageVC.h"
#import "HSSystemNoticeVC.h"

@interface HSMainMeVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ParallaxHeaderView *headerView;
@property (nonatomic, strong) UIImageView *headImgV;

@end

@implementation HSMainMeVC

- (instancetype)init
{
    if (self = [super init]) {
        self.title = @"设置";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = nil;
    [self.view addSubview:self.tableView];
    self.headerView = [ParallaxHeaderView parallaxHeaderViewWithImage:[UIImage imageNamed:@"bg_homepage"] forSize:CGSizeMake(ScreenWidth, 300)];
    [self.headerView.headerTitleLabel bk_whenTapped:^{
        [self rightButtonTouch];
    }];
    self.tableView.tableHeaderView = self.headerView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self updateView];
}

- (void)updateView
{
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:userNameKey];
    if (name) {
        self.headerView.headerIcon = [UIImage imageNamed:@"male"];
        self.headerView.headerTitleLabel.text = @"退出登录";//[NSString stringWithFormat:@"欢迎%@",name];
//        [self rightTitleWithTitle:@"退出登录"];
    }
    else{
        self.headerView.headerIcon = [UIImage imageNamed:@"unloginAvatar3"];
//        [self rightTitleWithTitle:@"登录"];
        self.headerView.headerTitleLabel.text = @"点击登录";
    }
    self.tableView.tableHeaderView = self.headerView;
    [self.tableView reloadData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.tableView)
    {
        // pass the current offset of the UITableView so that the ParallaxHeaderView layouts the subViews.
        [self.headerView layoutHeaderViewForScrollViewOffset:scrollView.contentOffset];
    }
}

- (void)rightButtonTouch
{
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:userNameKey];
    if (!name) {
        [self my_push:[HSLoginVC new]];
    }
    else{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:userNameKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self updateView];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        [cell showLineForDirection:MYDirectionBottom leftPad:17];
    }
    
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"我的收藏";
            if ([[NSUserDefaults standardUserDefaults] objectForKey:userNameKey]) {
                NSArray *data = [[MYCacheHelper shareHelper] my_cacheWithKey:collectionKey clazz:[NSArray class]];
                cell.textLabel.text = [NSString stringWithFormat:@"我的收藏  (%lu)", data.count];
            }
            break;
        case 1:
            cell.textLabel.text = @"我的话题";
            break;
        case 2:
            cell.textLabel.text = @"我的回复";
            break;
        case 3:
            cell.textLabel.text = @"我的消息";
            break;
        case 4:
            cell.textLabel.text = @"系统通知";
            break;
        case 5:
            cell.textLabel.text = @"设置";
            break;
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == 0){
        if (![[NSUserDefaults standardUserDefaults] objectForKey:userNameKey]) {
            //去登录
            [self my_push:[HSLoginVC new]];
        }
        else{
            //去收藏
            [self my_push:[HSCollectionVC new]];
        }
    }
    else if(indexPath.row < 4 ){
            //跳转我的消息
        if (![[NSUserDefaults standardUserDefaults] objectForKey:userNameKey]) {
            //去登录
            [self my_push:[HSLoginVC new]];
        }
        else{
            NSString *title = @"我的话题";
            if (indexPath.row == 2) {
                title = @"我的回复";
            }
            else if(indexPath.row == 3){
                title = @"我的消息";
            }
            [self my_push:[[HSMessageVC alloc] initWithTitle:title]];
        }
    }
    else if(indexPath.row == 4){
        //跳转系统通知
        [self my_push:[HSSystemNoticeVC new]];
    }
    else{
        //跳转设置
        [self my_push:[HSSettingVC new]];
    }
}

- (void)showMessage:(NSString *)msg
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - TabbarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


@end
